/*************************************************************************
    > File Name: 3.Thread_Shared_data_validation.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年05月09日 星期二 21时57分49秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<pthread.h>

int num = 100;
//线程处理函数
void *fun(void *arg)
{
	printf("before fun num = %d\n",num);
	num++;
	printf("after  fun num = %d\n",num);
	return NULL;
}

void *fun1(void *arg)
{
	int *pn = (int*)arg;
	printf("before fun *pn = %d\n",*pn);
	(*pn)++;
	printf("after  fun *pn = %d\n",*pn);

	return NULL;
}
//数据段和堆
int main()
{
	int ret = -1;
	pthread_t tid;
	pthread_t tid2;
	memset(&tid,0,sizeof(tid));

	int *p = NULL;
	//分配堆空间
	p = malloc(sizeof(int));
	if(NULL == p)
	{
		printf("malloc failed...\n");
		return 1;
	}
	memset(p,0,sizeof(int));
	*p = 88;

	//创建一个线程
	ret = pthread_create(&tid,NULL,fun,NULL);
	if(0 != ret)
	{
		printf("pthread_create failed...\n");
		return 0;
	}

	//创建一个线程
	ret = pthread_create(&tid2,NULL,fun1,(void *)p); 
	if(0 != ret)
	{
		printf("pthread_create failed...\n ");
		return 0;
	}

	printf("按下任意键 函数继续...\n");
	getchar();
	printf("main num = %d\n",num);

	free(p);
	return 0;
}
